package com.twu.biblioteca;


import java.util.ArrayList;
import java.util.Scanner;

public class Library {

    private User currentUser;
    private ArrayList<Movie> movies = new ArrayList();

    public void start() {
        setUp();
        showWelcome();
        while (true) {
            showMenu();
            selectOption(getInput());
        }
    }

    private void setUp() {
        movies.add(new Movie("Life of Pi", 2012, "Ang Lee", 9));
        movies.add(new Movie("3 Idiots", 2009, "Rajkumar Hirani", 8));
        movies.add(new Movie("Slumdog Millionaire", 2008, "Danny Boyle", 7));
        movies.add(new Movie("Edward Scissorhands", 1990, "Tim Burton", 8));
        movies.add(new Movie("Pulp Fiction", 1994, "Quentin Tarantino", 9));
        movies.add(new Movie("Children of Men", 2006, "Alfonso Cuarón", 7));
        movies.add(new Movie("Fight Club", 1999, "David Fincher", 9));
        movies.add(new Movie("Trainspotting", 1996, "Danny Boyle"));
        movies.add(new Movie("Back to the Future", 1985, "Robert Zemeckis", 6));
        movies.add(new Movie("Crash", 2004, "Paul Haggis", 8));

        new User("lsq", "123456", "foo@bar.com", "654321");
    }

    public void showWelcome() {
        System.out.println("Weclome to Bibloteca!");
    }

    public void showMenu() {
        System.out.println("Menu");
        System.out.println("1. List all books");
        System.out.println("2. Reserve a book");
        System.out.println("3. Check membership details");
        System.out.println("4. List all movies");
        if (loggedIn())
            System.out.println("5. Logout");
        else
            System.out.println("5. Login");
    }

    public void validOption() {
        System.out.println("Select a valid option!!");
    }

    public void listAllBooks() {
        System.out.println("1.Test Driven Development by Kent Beck");
        System.out.println("2.Thinking in Java by Bruce Eckel");
    }

    public void selectOption(int option) {
        switch (option) {
            case 1:
                listAllBooks();
                break;
            case 2:
                System.out.println("Please enter the ID of the book.");
                reserveBook(getInput());
                break;
            case 3:
                checkMembershipDetails();
                break;
            case 4:
                listAllMovies();
                break;
            case 5:
                logInOrLogOut();
                break;
            default:
                validOption();
        }
    }

    public void checkMembershipDetails() {
        if (loggedIn()) {
            System.out.println("Name:  " + currentUser.getUsername());
            System.out.println("Email: " + currentUser.getEmail());
            System.out.println("PhoneNumber: " + currentUser.getPhoneNumber());
            System.out.println("LibraryNumber: " + currentUser.getLibraryNumber());
        }
        else
            System.out.println("Please talk to a Librarian. Thank you.");
    }

    public void reserveBook(int bookId) {
        if (!loggedIn()) {
            System.out.println("You should login first");
            return;
        }
        if (bookId > 0 && bookId <= 2)
            System.out.println("Thank you! Enjoy the book.");
        else
            System.out.println("Sorry we don't have that book yet.");
    }

    private int getInput() {
        Scanner scanner = new Scanner(System.in);
        return scanner.nextInt();
    }

    private String getStringInput() {
        Scanner scanner = new Scanner(System.in);
        return scanner.next();
    }

    public void listAllMovies() {
        System.out.printf("%-20s%-6s%-20s%-3s\n", " Movie", "Year", "Director", "Rating");
        for (Movie movie : movies) {
            String rating = String.valueOf(movie.getRating());
            if (movie.getRating() == 0)
                rating = "N/A";
            System.out.print(movie.getName());
            System.out.printf("%s%-20s%-6s%-20s%-3s\n", "*", movie.getName(), movie.getYear(), movie.getDirector(), rating);
        }
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void logIn(String username, String password) {
        currentUser = User.logIn(username, password);
    }

    public void logOut() {
        currentUser = null;
    }

    private boolean loggedIn() {
        return !(currentUser == null);
    }

    private void logInOption() {
        System.out.println("Please enter the username or library number");
        String username = getStringInput();
        System.out.println("Please enter the password");
        String password = getStringInput();
        logIn(username, password);
        if (!loggedIn())
            System.out.println("The username/password is invalid");
    }

    private void logInOrLogOut() {
        if (loggedIn())
            logOut();
        else
            logInOption();
    }
}
